package com.jeegit.common.utils;





/**
 * @author taoist-infoIdUtils
 * @since 5.0.1
 */
public class IdUtils {

	/**
	 * 基于Twitter的Snowflake算法实现分布式高效有序ID生产黑科技(sequence)——升级版Snowflake
	 */
	private  static  Snowflake snowflake= null;
	
	/**
	 * 静态工厂方法 获取当前对象实例 多线程安全单例模式实例三(使用双重同步锁)
	 * 
	 * @return 全局配置Global
	 * @author 长春叭哥
	 */
	public static synchronized Snowflake getInstance(long dataCenterId) {
		if (snowflake == null) {
			synchronized (Snowflake.class) {
				if (snowflake == null)
					snowflake = new Snowflake(dataCenterId);
			}
		}
		return snowflake;
	}




	public static synchronized String[] getEntityIdField(Object entity)
	{
	return Reflections.getFieldValue(entity, "PK_FIELDS").toString().split(",");
	}
	
	public static synchronized String getEntityIdFieldValue(Object entity,int index)
	{
	return	Reflections.getFieldValue(entity, getEntityIdField(entity)[index]).toString();
	}
	
	public static synchronized String getEntityIdsFieldValues(Object entity)
	{
	    	String[] idFields=IdUtils.getEntityIdField(entity);
	        StringBuilder sbIds=new StringBuilder();
		if(idFields.length>1) {
		    for (int i = 0; i < idFields.length; i++) {
			sbIds.append(IdUtils.getEntityIdFieldValue(entity,i));
			if((i+1)<=idFields.length) {
			    sbIds.append(",");
			}
		    }
		}else {
		    sbIds.append(IdUtils.getEntityIdFieldValue(entity,0));
		}
		return sbIds.toString();
	}

	
	
	
}
